package com.cy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cy.common.entity.Result;
import com.cy.mapper.UserRoleMapper;
import com.cy.pojo.Role;
import com.cy.pojo.User;
import com.cy.pojo.UserRole;
import com.cy.pojo.vo.SearchVo;
import com.cy.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.role;

/**
 * @description 角色信息
 * @author 残影
 * @date 2022-04-13
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色信息模块")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 根据ID查询角色信息
     */
    @GetMapping("{id}")
    @ApiOperation("根据ID查询角色信息")
    public Result findById(@PathVariable Integer id) {
        Role role = roleService.getById(id);
        if (role != null) {
            return Result.ok("查询角色信息成功", role);
        } else {
            return Result.failed(201, "没有该角色信息");
        }
    }

    /**
    * 分页查询角色信息
    */
    @ApiOperation("分页查询角色信息")
    @GetMapping("/list")
    public Result list(SearchVo searchVo) {
        //分页构造器
        Page<Role> buildPage = new Page<>(searchVo.getPage(), searchVo.getLimit());
        //条件构造器
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(searchVo.getName())) {
            queryWrapper.like("role_name", searchVo.getName());
        }
        IPage<Role> pageList = roleService.page(buildPage, queryWrapper);
        return Result.ok("查询成功", pageList.getRecords()).put("total", pageList.getTotal());
    }

    /**
     * 查询所有角色信息
     */
    @ApiOperation("查询所有角色信息")
    @GetMapping("/listAll")
    public Result listAll() {
        List<Role> roleList = roleService.list();
        return Result.ok("查询成功", roleList);
    }

//    /**
//     * 保存角色信息
//     */
//    @PostMapping
//    @ApiOperation("保存角色信息")
//    public Result save(@RequestBody Role role) {
//        role.setCreateTime(new Date());
//        role.setUpdateTime(new Date());
//        boolean b = roleService.save(role);
//        if (b) {
//            return Result.ok("保存用户角色信息成功", role);
//        } else {
//            return Result.failed("保存角色信息失败");
//        }
//    }

    /**
     * 修改用户角色
     */
    @PutMapping("updateUserRole")
    @ApiOperation("修改用户角色")
    public Result update(@RequestBody UserRole userRole) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userRole.getUserId());
        UserRole oldUserRole = userRoleMapper.selectOne(queryWrapper);
        oldUserRole.setRoleId(userRole.getRoleId());
        userRoleMapper.updateById(oldUserRole);
        return Result.ok("修改用户角色成功");
    }
}
